using System;
using System.Diagnostics;

namespace OpenRay
{
	//Immutable class
	public class GeometryHit : IComparable
	{	
		private readonly Geometry geometry;
		private readonly Ray modelRay;
	
		public float Distance {
			get;
			private set;
		}
		
		private readonly InterpolationData interpolationData;
		
		public GeometryHit ( Geometry g, Ray r, float d )
		{
			geometry = g;
			modelRay = r;
			
			Distance = d;
			interpolationData = null;
		}
		
		public GeometryHit ( Geometry g, Ray r, float d, InterpolationData interp )
		{			
			geometry = g;
			modelRay = r;
			Distance = d;
			interpolationData = interp;
		}
		
		public int CompareTo(Object obj)
		{
			if ( obj == null )
				return -1;
			
			GeometryHit temp = obj as GeometryHit;
			if (temp != null )
				return Distance.CompareTo( temp.Distance );
			else
				throw new ArgumentException( "Object is not a Hit" );
		}
		
		public Point ModelHit()
		{
			return modelRay.Origin + modelRay.Direction * Distance;	
		}
		
		public Material Material(Point modelHit)
		{
			return geometry.Material(modelHit, interpolationData);	
		}
		
		public Vector Normal(Point modelHit)
		{
			return geometry.Normal(modelHit, interpolationData);	
		}
	}
}

